<?php
namespace Admin\Model;
use Think\Model;

class ProjectModel extends AbstractModel {

	protected $results = array();
	protected $HisVer = array();
	protected $HisVerArr;
	protected $ProjectID;
	
	public $_validate = array(
        array('Name','require','请输入项目名称',Model::MUST_VALIDATE),
        array('Customer','require','请输入客户名称',Model::MUST_VALIDATE),
        array('Status','require','请输入项目状态',Model::MUST_VALIDATE)
    );
	
	protected $_auto = array(
    	array('ProjectNO', 'getNowNO',1,'callback'),
    	array('InputUserID','getUserID',1,'callback'),
    	array('InputTime', 'getTime',1,'callback'),
    	array('updatetime', 'getTime',2,'callback')
  	);
  
	public function getTime(){
		return date('Y-m-d H:i:s');
	}

	public function getUserID(){
		return session('USER_ID');
	}
	
	public function getNowNO(){
		$date = date("ym");
		$serialno = M('serialno');
		$sql = "SELECT NowNO FROM serialno WHERE Name = 'project' AND NowNO LIKE '%{$date}%' ORDER BY ID DESC";
		$res = $serialno->query($sql);
		if(!empty($res)){
			//$nowNo = ($res[0]['NowNO'] + 10000001) - 10000000;
            $nowNo = $res[0]['NowNO'] + 1;
		}else{
			$nowNo = date('ymd')."01";
		}
		$data['Name'] = 'project';
		$data['NowNO'] = $nowNo; 
		$id = $serialno->add($data);
		return $id;
	}
	
	public function Myupdata(){
		if(!$this->create()){
			$this->results['msg'] = $this->getError();
			$this->results['state'] = 0;
		}else{
			$res = $this->save();
			if($res) {
				$this->results['state'] = 1;
				$this->results['msg'] = "修改成功！";
				$this->results['id'] = $res;
			}else{
				$this->results['state'] = 0;
				$this->results['msg'] = "修改错误！";
			}
		}
		return $this->results;
	}
	
	public function Mydelet(){
		$dbName = $this->getTableName();
		$ID = I("ID");
		$sql = " UPDATE {$dbName} SET IsDel = 1 WHERE ID = {$ID}";
		$res = $this->execute($sql);
		if($res){
			$this->results['state'] = 1;
			$this->results['msg'] = "删除成功！";
			$this->results['id'] = $res;
		}else{
			$this->results['state'] = 0;
			$this->results['msg'] = "删除错误！";
		}
		return $this->results;
	}
	
	public function Myshow($parmArr){
		$where = " 1 ";
		foreach($parmArr AS $key => $value){
			$where .= " AND $key = '$value'"; 
		}
		$sql = "SELECT *,(SELECT Name FROM department WHERE ID = a.DepID) AS DepName, (SELECT UserName FROM user WHERE ID = a.ApplyUserID) AS ApplyUserName, (SELECT UserName FROM user WHERE ID = a.ChargeUserID) AS ChargeUserName, (SELECT UserName FROM user WHERE ID = a.LeaderUserID) AS LeaderUserName FROM project AS a WHERE $where";
		$res = $this->query($sql);
		return $res;
	}
	
	public function MyshowList(){
		$where = " 1 ";
		$keyword = I('keyword');
		if(!empty($keyword)){
			$where .= " AND Name like '%$keyword%' ";
		}
		$return = array();
		$sql = " SELECT COUNT(*) count FROM project WHERE $where ";
		$count = $this->query($sql);
		$count = $count[0]['count'];
	    $page = $this->Mypage($count, 8);
		$sql = "SELECT ID,Name,(SELECT NowNO FROM serialno WHERE ID = a.ProjectNO) AS ProjectNO,(SELECT Name FROM department WHERE ID = a.DepID) AS DepID,(SELECT Name FROM datadict WHERE ID = a.ProjectType) AS ProjectType FROM project AS a WHERE IsDel = 0 AND $where ORDER BY ID DESC LIMIT  $page->firstRow , $page->listRows";
		$res = $this->query($sql);
		$return['list'] = $res;
		$return['page'] = $page->show('Admin');
		return $return;
	}
	
	public function getTaskList(){
		$res = M("task")->select();
		return $res;
	}
	
	
	public function compareProject($in){
		$this->ProjectID = $in['ID'];
		$resa = M("task")->field("Name")->where(array("ID"=>$in['TID']))->find();
		$resb = M("datadict")->field("Name")->where(array("ID"=>$in['ProjectType']))->find();
		$resc = M("datadict")->field("Name")->where(array("ID"=>$in['Scale']))->find();
		$resd = M("datadict")->field("Name")->where(array("ID"=>$in['FundSource']))->find();
		$rese = M("datadict")->field("Name")->where(array("ID"=>$in['Status']))->find();
		$in['ProjectTypeName'] = $resb['Name'];
		$in['ScaleName'] = $resc['Name'];
		$in['FundSourceName'] = $resd['Name'];
		$in['StatusName'] = $rese['Name'];
		$in['taskName'] = $resa['Name'];
		$field = "a.*, (SELECT Name FROM datadict WHERE ID = a.Status) AS StatusName, (SELECT Name FROM datadict WHERE ID = a.FundSource) AS FundSourceName, (SELECT Name FROM datadict WHERE ID = a.Scale) AS ScaleName, (SELECT Name FROM datadict WHERE ID = a.ProjectType) AS ProjectTypeName, (SELECT Name FROM task WHERE ID = a.TID) AS taskName, (SELECT Name FROM department WHERE ID = a.DepID) AS DepName, (SELECT UserName FROM user WHERE ID = a.ApplyUserID) AS ApplyUserName, (SELECT UserName FROM user WHERE ID = a.ChargeUserID) AS ChargeUserName, (SELECT UserName FROM user WHERE ID = a.LeaderUserID) AS LeaderUserName";
		$res = $this->alias('a')->field($field)->where(array("a.ID"=>$in['ID']))->find();
		unset($res['ProjectNO']);
		unset($res['InputUserID']);
		unset($res['InputTime']);
		unset($res['updatetime']);
		unset($res['TID']);
		unset($res['ProjectType']);
		unset($res['Scale']);
		unset($res['FundSource']);
		unset($res['DepID']);
		unset($res['ChargeUserID']);
		unset($res['Status']);
		unset($res['ApplyUserID']);
		unset($res['IsDel']);
		unset($res['LeaderUserID']);
		unset($res['ID']);
		
		unset($in['TID']);
		unset($in['ProjectType']);
		unset($in['Scale']);
		unset($in['FundSource']);
		unset($in['DepID']);
		unset($in['ChargeUserID']);
		unset($in['Status']);
		unset($in['ApplyUserID']);
		unset($in['IsDel']);
		unset($in['LeaderUserID']);
		unset($in['ID']);
		foreach($res AS $k => $v){
			if($in[$k] != $v){
				$this->HisVer[$k] = $v;
			}	
		}
		foreach($in AS $k => $v){
			$this->HisVerArr[$k.'1'] = $v;
		}
		foreach($res AS $k => $v){
			$this->HisVerArr[$k.'2'] = $v;
		}
		unset($in);
		unset($res);
	}
	
	public function Snapshot(){
		if(!empty($this->HisVer)){
			$res = M("projecthisver")->field("Version")->where(array("ProjectID"=>$this->ProjectID))->order("ID DESC")->find();
			$Version = $res['Version'] ? $res['Version'] + 1 : 0 + 1;
			$this->HisVerArr['Version1'] = $this->HisVerArr['Name1']." 版本 ".$Version;
			$this->HisVerArr['Version2'] = $this->HisVerArr['Name1']." 版本 ".($Version-1);
			$Snapshot = serialize($this->HisVerArr);
			$UserID = $this->getUserID();
			$AddTime = $this->getTime();
			M("projecthisver")->add(array(
			"ProjectID"=>$this->ProjectID,
			"Version"=>$Version,
			"Snapshot"=>$Snapshot,
			"UserID"=>$UserID,
			"AddTime"=>$AddTime,
			));
		}
	}
	
}

?>				